package com.fatecpg.fightscore;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class CadastroLutadoresActivity extends ActionBarActivity {
	//instanciando objetos dbHelper e db 
	FightScoreSQLiteHelp dbHelper = null;
	 SQLiteDatabase db = null;
	 
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastro_lutadores);
		//try catch para achar os erros
		try{
			dbHelper = new FightScoreSQLiteHelp(getApplicationContext());
		}catch(Exception ex){
			new AlertDialog.Builder(this).setMessage(ex.getMessage()).setPositiveButton("OK", null).show();
		}
		
		
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}
	
	//metodo limpar o editex
	public void limpar(View v){
		
		((EditText) findViewById(R.id.editNomeLutador)).setText("");
		((EditText) findViewById(R.id.editIdadeLutador)).setText("");
		((EditText) findViewById(R.id.editPesoLutador)).setText("");
			
	}
	//metodo para fechar a activity
	public void cancelar(View v){		
		finish();
	}
	
	//metodo para inserir dados no banco
	public void addClick(View v){
		try{
			EditText editNome = (EditText) findViewById(R.id.editNomeLutador);
			String nome = editNome.getText().toString();
			EditText editIdade = (EditText) findViewById(R.id.editIdadeLutador);
			int idade = Integer.parseInt(editIdade.getText().toString());
			EditText editPeso = (EditText) findViewById(R.id.editPesoLutador);
			double peso = Double.parseDouble(editPeso.getText().toString());
			
			db = dbHelper.getWritableDatabase();
			db.execSQL(" INSERT INTO LUTADOR(NOME, IDADE, PESO) VALUES('"+nome+"', "+idade+" ,"+peso+") ");
			db.close();
			dbHelper.close();
			new AlertDialog.Builder(this).setMessage("Cadastrado com Sucesso!").setPositiveButton("OK", null).show();
			limpar(v);
		}catch(Exception ex){
			new AlertDialog.Builder(this).setMessage(ex.getMessage()).setPositiveButton("OK", null).show();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cadastro_lutadores, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_cadastro_lutadores, container, false);
			return rootView;
		}
	}

}
